﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using eLibServer.Core;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Security.Principal;

namespace eLibServerMonitor
{
  public partial class frmServerMonitor : Form
  {
    private ConnectionHandler _connectionHandler;
    private delegate void AppendTextDelegate(string text);
    private AppendTextDelegate _statusAppender;

    public frmServerMonitor()
    {
      InitializeComponent();

      _connectionHandler = new ConnectionHandler();
      _connectionHandler.OnConnectionAccepted += new OnConnectionAcceptedHandler(OnConnectionAccepted);
      _connectionHandler.OnStartListening += new OnStartListeningHandler(OnStartListening);
      _connectionHandler.OnStopListening += new OnStopListeningHandler(OnStopListening);
      _connectionHandler.OnConnectionClosed += new OnClientDisconnectHandler(OnConnectionClosed);
      
      _statusAppender = new AppendTextDelegate(AppendStatusText);  
    }

    private void btnStart_Click(object sender, EventArgs e)
    {
      int port;
      if ( int.TryParse(txtServerPort.Text, out port) )
        if (_connectionHandler != null)
          _connectionHandler.StartListening(port);
    }

    private void btnStop_Click(object sender, EventArgs e)
    {
      if (_connectionHandler != null)
        _connectionHandler.StopListening();
    }



  #region server event handlers 

    private void AppendStatusText(string statusText)
    {
      txtStatus.AppendText(statusText);
    }

    private void OnConnectionAccepted(object sender, ConnectionAcceptedEventArgs e)
    {
      string message = string.Format("client connection accepted from {0}\r\n", e.RemoteEndPoint.Address);
      txtStatus.Invoke(_statusAppender, new Object[] { message });
    }

    private void OnStopListening(object sender, EventArgs e)
    {
      txtStatus.AppendText("stopped listening\r\n");
    }

    private void OnStartListening(object sender, StartListeningEventArgs e)
    {
      txtStatus.AppendText(string.Format("started listening on port {0}\r\n", e.Port.ToString()));
    }

    private void OnConnectionClosed(object sender, OnClientDisconnectEventArgs e)
    {
      string message = string.Format("client disconnected on {0}\r\n", e.RemoteEndPoint.Address);
      txtStatus.Invoke(_statusAppender, new Object[] { message });
    }

  #endregion

    private void frmServerMonitor_FormClosing(object sender, FormClosingEventArgs e)
    {
      _connectionHandler.OnConnectionAccepted -= new OnConnectionAcceptedHandler(OnConnectionAccepted);
      _connectionHandler.OnStartListening -= new OnStartListeningHandler(OnStartListening);
      _connectionHandler.OnStopListening -= new OnStopListeningHandler(OnStopListening);
      _connectionHandler.OnConnectionClosed -= new OnClientDisconnectHandler(OnConnectionClosed);
    
    }

    private void btnSpawnClient_Click(object sender, EventArgs e)
    {
      string clientPath = @"..\..\..\eLibClient\bin\Debug\eLibClient.exe";
      ProcessStartInfo s = new ProcessStartInfo(clientPath);
      Process p = new Process();
      p.StartInfo = s;
      p.Start();

    }







  }
}
